package com.xiaomai.eduorder.service.impl;

import com.xiaomai.commonutil.ordervo.CourseOrderVo;
import com.xiaomai.commonutil.ordervo.UcenterOrderVo;
import com.xiaomai.eduorder.client.EduClient;
import com.xiaomai.eduorder.client.UcenterClient;
import com.xiaomai.eduorder.entity.Order;
import com.xiaomai.eduorder.mapper.OrderMapper;
import com.xiaomai.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaomai.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-17
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private UcenterClient ucenterClient;
    @Autowired
    private EduClient eduClient;
    @Override
    public String createOrders(String courseId,String memberId) {
        //根据用户Id获取用户信息
        UcenterOrderVo userInfoOrder = ucenterClient.getUserInfo(memberId);

        //根据课程Id获取课程信息
        CourseOrderVo orderCourseInfo = eduClient.getOrderCourseInfo(courseId);
        //根据用户Id获取用户信息
//        UcenterOrderVo userInfo = ucenterClient.getUserInfo(memberIdByJwtToken);


        //创建一个订单对象
        Order order = new Order();
        //将获取到的信息都赋值给订单对象
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(orderCourseInfo.getTitle());
        order.setCourseCover(orderCourseInfo.getCover());
        order.setTeacherName("test");
        order.setTotalFee(orderCourseInfo.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        //将对象添加进数据库
        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }
}
